//
//  CourseViewController.m
//  Four_Fit_New
//
//  Created by xalo on 16/7/14.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "CourseViewController.h"
#import "CourseModel.h"
#import "CourseCell.h"
#import "NewView.h"

#import "TrainViewController.h"

@interface CourseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)UIView *navBackView;

@end

@implementation CourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.navigationItem.title = self.myTitle;
    
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CourseCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"Course_Cell"];
    


}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    //导航栏背景色
#warning 导航栏颜色.........................................................
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.27 green:0.29 blue:0.33 alpha:1.00];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loadData{
    NSString *url = [NSString stringWithFormat:@"http://bea.sportq.com/SFitWeb/sfit/getClassifyPlan?form.classifyId=%@&form.device=&form.mySex=0&form.phoneType=1&form.sex=0&form.sign=2B8ADA59D76837C77A6BC6E5C5F07A12&form.userId=93118&form.version=1.1.5",self.classifyId];
#warning 数据写了缓存 ...  记得关闭
    [[QZRequestManager sharedManager] fetchDataWithUrl:url parameters:nil method:@"GET" needToCaches:YES fetchData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic1 in dic[@"entRet"][@"lstIndividual"]) {
            CourseModel *model = [[CourseModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataSource addObject:model];
        }
    } updateUI:^{
        
        [self.collectionView reloadData];
    }];
}
#pragma mark  属性懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        //设置Layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kWidth - 1) / 2.0, (kWidth - 1) / 2.0);
        layout.minimumInteritemSpacing = 1.0;
        layout.minimumLineSpacing = 1.0;
        //collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}
#pragma mark  collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CourseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Course_Cell" forIndexPath:indexPath];
    //第一个和第二个 添加new 
    if (indexPath.row == 0 || indexPath.row == 1) {
        [cell addSubview:[[NewView alloc] initWithFrame:CGRectMake(cell.frame.size.width - cell.frame.size.width / 3.6, 0, cell.frame.size.width / 3.6, cell.frame.size.height / 3.6)]];
    }
    
    [cell setCellWithModel:self.dataSource[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TrainViewController *trainVC = [[TrainViewController alloc] initWithNibName:@"TrainViewController" bundle:[NSBundle mainBundle]];
    trainVC.planId = [self.dataSource[indexPath.row] planId];
    trainVC.myTitle = [self.dataSource[indexPath.row] planName];
    [self.navigationController pushViewController:trainVC animated:YES];
}
@end
